package net.hcdz.identity.service.impl;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import net.hcdz.identity.bean.Files;
import net.hcdz.identity.bean.User;
import net.hcdz.identity.dao.impl.FilesDao;
import net.hcdz.identity.dao.impl.UserDao;
import net.hcdz.identity.service.IdentityServiceI;
import net.hcdz.identity.util.MD5;


@Service
@Transactional(readOnly=false,rollbackFor=java.lang.Exception.class)
public class IdentityService implements IdentityServiceI{

	@Autowired
	private UserDao userDao;
	@Autowired
	private FilesDao filesDao;
	
	
	@Transactional(readOnly=true)
	@Override
	public User getUserById(String userId) {
		try {
			List<User> users = userDao.getUserByName(userId);
			return users.get(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	//创建用户
	@Override
	public void savaUser(User user) {
		try {
			userDao.save(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//
	@Override
	public List<String> getUserNameAll() {
		try {
			return userDao.getUserByIdAll();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("");
		}
		return null;
	}
	//查询所以用户
	@Override
	public List<User> selectUser() {
		try {
			return userDao.getUserAll();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	//
	@Override
	public String updateUser(User user) {
		try {
			User u = userDao.get(User.class, user.getUserId());
			if(user.getPassWord().equals(u.getPassWord())){
				u.setPassWord(user.getPassWord());
			}else{
				String passwordx = MD5.EncoderByMd5(user.getPassWord());
				u.setPassWord(passwordx);
			}
			u.setDelFlag("1");
			//u.setUserLanguage(user.getUserLanguage());
			u.setUserMailBox(user.getUserMailBox());
			u.setUserPhoneNumBer(user.getUserPhoneNumBer());
			//u.setUserRace(user.getUserRace());
			u.setUserSex(user.getUserSex());
			return "";
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
	@Override
	public void deleteUserByIds(String userId) {
		try {
			User u = userDao.get(User.class, userId);
			u.setDelFlag("0");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	
	@Override
	public boolean ajaxValidName(String userId) {
		try {
			User u = userDao.get(User.class, userId);
			if(u!=null){
				return true;
			}else{
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	//判断文件名是否重复
	@Override
	public List<Files> Duplicatefiles(String filename,String files) {
		try {
			return filesDao.repeatfile(filename,files);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void savaFiles(List<Files> filess) {
		
		try {
			for(int i=0;i<filess.size();i++){
				Files files = filess.get(i);
				filesDao.save(files);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	

}